package com.ymatou.userservice.testcase;

import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.userservice.parameter.AddressRequestDtoBean;
import com.ymatou.userservice.service.AddressRequestDtoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.UserCallService;
import com.ymttest.database.sqlwapper.YmtAddressWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;

/**
 * 收货地址上限调整为20个 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_AddressRequestDto {

	private static AddressRequestDtoBean arb;
	private static AddressRequestDtoCall arc;
	private static YmtAddressWapper addressWapper;
	private static final int ADDRESS_NUM = 20;
	private static final String REQUEST_ADD = "Add";
	private static final String REQUEST_UPDATE = "Update";
	private static final String REQUEST_SETDEFAULT = "SetDefault";
	private static final String REQUEST_MARKDELETE = "MarkDelete";
	private static final int ADDRESS_ACTIVE = 0;
	private static final int ADDRESS_DELETED = -1;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("收货地址上限调整为20个 ");
	}

	@Before
	public void caseUp() {
		arb = new AddressRequestDtoBean();
		arc = new AddressRequestDtoCall();
		addressWapper = new YmtAddressWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(arc.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	@Ignore
	//userservice调整为200个，前端调整为20个，忽略此case
	public void Tc_AddressRequestDto_001() {
		Logger.start(true,"修改收货地址上限调整为20个成功");
		try {
			Map user = UserCallService.registerUser(null, TestDataManager.getPhoneNum(),null);
			int userid = (Integer)user.get("userId");

			for (int i = 0; i < ADDRESS_NUM; i++) {
				arc = new AddressRequestDtoCall();
				arb.setiAddressId(i);
				arb.setiUserid(userid);
				arb.setsAddressCode("100101");
				arb.setsBaseAddress("autotest" + userid + i);
				arb.setsDetailAddress("autotest" + userid + i);
				arb.setiAction(ADDRESS_ACTIVE);
				arb.setsPostCode("100100");
				arb.setsTelphone("100100");
				arb.setsMobilePhone(TestDataManager.getPhoneNum());
				arb.setsRecipient("autotest");
				arb.setsEmail("autotest@ymatou.com");
				arb.setbIsDefault(false);
				arb.setbXloboChecked(false);
				arb.setAction(REQUEST_ADD);

				arc.setData(arb);
				arc.callService();

				Logger.verifyEquals(true, arc.getSuccess(), "添加地址成功");
				Logger.verifyEquals("ok", arc.getString("SuccessMessage"), "验证Msg信息");
				Logger.verifyEquals(ECode.SUCCESS, arc.getString("ResponseCode"), "验证Code");
				Map address = addressWapper.selectAdressByUserIdAndAddress(userid, "autotest" + userid + i, "autotest" + userid + i);
				Logger.verifyEquals(address.get("iAddressId").toString(), arc.getString("Result").toString(), "验证Addressid");
			}
			arc = new AddressRequestDtoCall();
			arb.setiAddressId(21);
			arb.setiUserid(userid);
			arb.setsAddressCode("100101");
			arb.setsBaseAddress("autotest" + userid + 21);
			arb.setsDetailAddress("autotest" + userid + 21);
			arb.setiAction(ADDRESS_ACTIVE);
			arb.setsPostCode("100100");
			arb.setsTelphone("100100");
			arb.setsMobilePhone("13888888888");
			arb.setsRecipient("autotest");
			arb.setsEmail("autotest@ymatou.com");
			arb.setbIsDefault(false);
			arb.setbXloboChecked(false);
			arb.setAction(REQUEST_ADD);

			arc.setData(arb);
			arc.callService();

			Logger.verifyEquals(false, arc.getSuccess(), "达到20个上线后在添加，失败");
			Logger.verifyEquals("地址已达到数量上限", arc.getLastErrorMessage(), "达到20个上线后在添加提示地址已达到数量上限");
			Logger.verifyEquals(ECode.ERROR400, arc.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("-1", arc.getString("Result"), "验证result信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_AddressRequestDto_002() {
		Logger.start(true,"设置默认地址，成功");
		try {
			Map user = UserCallService.registerUser(null, TestDataManager.getPhoneNum(),null);
			int userid = (Integer)user.get("userId");

			Logger.comment("给用户"+userid+"添加3个地址");
			for (int i = 0; i < 3; i++) {
				arc = new AddressRequestDtoCall();
				arb.setiAddressId(i);
				arb.setiUserid(userid);
				arb.setsAddressCode("100101");
				arb.setsBaseAddress("autotest" + userid + i);
				arb.setsDetailAddress("autotest" + userid + i);
				arb.setiAction(ADDRESS_ACTIVE);
				arb.setsPostCode("100100");
				arb.setsTelphone("100100");
				arb.setsMobilePhone("13888888888");
				arb.setsRecipient("autotest");
				arb.setsEmail("autotest@ymatou.com");
				arb.setbIsDefault(false);
				arb.setbXloboChecked(false);
				arb.setAction(REQUEST_ADD);

				arc.setData(arb);
				arc.callService();
				Logger.verifyEquals(true, arc.getSuccess(), "添加地址成功");
//				Logger.verifyEquals("ok", arc.getString("SuccessMessage"), "验证Msg信息");
//				Logger.verifyEquals(ECode.SUCCESS, arc.getString("ResponseCode"), "验证Code");
//				Map address = addressWapper.selectAdressByUserIdAndAddress(userid, "autotest" + userid + i, "autotest" + userid + i);
//				Logger.verifyEquals(address.get("iAddressId").toString(), arc.getString("Result").toString(), "验证Addressid");
			}
			Map address = addressWapper.selectAdressByUserIdAndAddress(userid, "autotest" + userid + 1, "autotest" + userid + 1);
			Logger.comment("设置地址2为默认地址");
			arc = new AddressRequestDtoCall();
			arb.setiAddressId((Integer)address.get("iAddressId"));
			arb.setiUserid(userid);
			arb.setiAction(ADDRESS_ACTIVE);//action为2表示设置默认地址
			arb.setsEmail("autotest@ymatou.com");
			arb.setAction(REQUEST_SETDEFAULT);
			arb.setbIsDefault(true);
			arc.setData(arb);
			arc.callService();
			
			Logger.verifyEquals(ECode.SUCCESS, arc.getString("ResponseCode"), "验证code");
			Logger.verifyEquals("true", arc.getString("Success"), "验证操作是否成功");
			Logger.verifyEquals("提交成功", arc.getString("LastErrorMessage"), "验证Msg信息");
			Logger.verifyEquals(3, (Double.valueOf(arc.getString("Result"))).intValue(), "验证result信息" );
			address = addressWapper.selectAdressByUserIdAndAddress(userid, "autotest" + userid + 1, "autotest" + userid + 1);
			Logger.verifyEquals(true, address.get("bIsDefault"), "验证此地址被设置为默认地址");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_AddressRequestDto_003() {
		Logger.start(false,"添加已存在的地址，失败");
		try {
			arb.setiAddressId(1);
			arb.setiUserid(1);
			arb.setsAddressCode("100101");
			arb.setsBaseAddress("autotest");
			arb.setsDetailAddress("autotest");
			arb.setiAction(ADDRESS_ACTIVE);
			arb.setsPostCode("100100");
			arb.setsTelphone("100100");
			arb.setsMobilePhone("13888888888");
			arb.setsRecipient("autotest");
			arb.setsEmail("autotest@ymatou.com");
			arb.setbIsDefault(false);
			arb.setbXloboChecked(false);
			arb.setAction(REQUEST_ADD);

			arc.setData(arb);
			arc.callService();
			Logger.verifyEquals(false, arc.getSuccess(), "验证添加失败");
			Logger.verifyEquals("存在相同的地址", arc.getLastErrorMessage(), "用户重复添加同一地址提示已存在此地址");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_AddressRequestDto_004() {
		Logger.start(false,"不输入用户Id，失败");
		try {
			arb.setiAddressId(100);
			arb.setiUserid(null);
			arb.setsAddressCode("100101");
			arb.setsBaseAddress("autotest");
			arb.setsDetailAddress("autotest");
			arb.setiAction(ADDRESS_ACTIVE);
			arb.setsPostCode("100100");
			arb.setsTelphone("100100");
			arb.setsMobilePhone("13888888888");
			arb.setsRecipient("autotest");
			arb.setsEmail("autotest@ymatou.com");
			arb.setbIsDefault(false);
			arb.setbXloboChecked(false);
			arb.setAction(REQUEST_ADD);

			arc.setData(arb);
			arc.callService();
			Logger.verifyEquals(false, arc.getSuccess(), "验证添加失败");
			Logger.verifyEquals("存在相同的地址", arc.getLastErrorMessage(), "用户重复添加同一地址提示已存在此地址");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_AddressRequestDto_005() {
		Logger.start(true,"删除地址，成功");
		try {
			
			Map user = UserCallService.registerUser(null, TestDataManager.getPhoneNum(),null);
			int userid = (Integer)user.get("userId");

			Logger.comment("给用户"+userid+"添加1个收货地址");
			arc = new AddressRequestDtoCall();
			arb.setiAddressId(1);
			arb.setiUserid(userid);
			arb.setsAddressCode("100101");
			arb.setsBaseAddress("autotest" + userid);
			arb.setsDetailAddress("autotest" + userid);
			arb.setiAction(ADDRESS_ACTIVE);
			arb.setsPostCode("100100");
			arb.setsTelphone("100100");
			arb.setsMobilePhone("13888888888");
			arb.setsRecipient("autotest");
			arb.setsEmail("autotest@ymatou.com");
			arb.setbIsDefault(false);
			arb.setbXloboChecked(false);
			arb.setAction(REQUEST_ADD);
			arc.setData(arb);
			arc.callService();
			Logger.verifyEquals(true, arc.getSuccess(), "添加地址成功");
			Logger.verifyEquals("ok", arc.getString("SuccessMessage"), "验证Msg信息");
			Logger.verifyEquals(ECode.SUCCESS, arc.getString("ResponseCode"), "验证Code");

			Logger.comment("将该收货地址标志为删除");
			Map address = addressWapper.selectAdressByUserIdAndAddress(userid, "autotest" + userid, "autotest" + userid);
			arb.setiAddressId((Integer)address.get("iAddressId"));
			arb.setiUserid(userid);
			arb.setsAddressCode("100101");
			arb.setsBaseAddress("autotest" + userid);
			arb.setsDetailAddress("autotest" + userid);
			arb.setiAction(ADDRESS_DELETED);
			arb.setsPostCode("100100");
			arb.setsTelphone("100100");
			arb.setsMobilePhone("13888888888");
			arb.setsRecipient("autotest");
			arb.setsEmail("autotest@ymatou.com");
			arb.setbIsDefault(false);
			arb.setbXloboChecked(false);
			arb.setAction(REQUEST_MARKDELETE);
			arc.setData(arb);
			arc.callService();
			Logger.verifyEquals(true, arc.getSuccess(), "验证添加失败");
			address = addressWapper.selectAdressByUserIdAndAddress(userid, "autotest" + userid, "autotest" + userid);
			Logger.verifyEquals(-1, (Integer)address.get("iAction"), "验证地址目前是删除状态");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_AddressRequestDto_006() {
		Logger.start(true,"更新地址信息，成功");
		try {
			
			Map user = UserCallService.registerByMobile();
			int userId = (Integer)user.get("userid");
			List<Map> address = addressWapper.getAdressByUserId(userId);
			String moblie=TestDataManager.getPhoneNum();
			arc = new AddressRequestDtoCall();
			arb.setiAddressId((Integer)address.get(0).get("iAddressId"));
			arb.setiUserid(userId);
			arb.setsAddressCode("100188");
			arb.setsBaseAddress("autotest" + userId+"updated");
			arb.setsDetailAddress("autotest" + userId+"detailupdated");
			arb.setiAction(ADDRESS_ACTIVE);
			arb.setsPostCode("100112");
			arb.setsTelphone("100101");
			arb.setsMobilePhone(moblie);
			arb.setsRecipient("autotest");
			arb.setsEmail("updatedemail@ymatou.com");
			arb.setbIsDefault(true);
			arb.setAction(REQUEST_UPDATE);
			arc.setData(arb);
			arc.callService();
			Logger.verifyEquals(true, arc.getSuccess(), "修改地址成功");
			Logger.verifyEquals("ok", arc.getString("SuccessMessage"), "验证Msg信息");
			Logger.verifyEquals(ECode.SUCCESS, arc.getString("ResponseCode"), "验证Code");

			address = addressWapper.getAdressByUserId(userId);
			Logger.verifyEquals(1, address.size(), "验证只有一条地址信息");
			Map addressInfo = address.get(0);
			Logger.verifyEquals("100101", addressInfo.get("sTelphone"), "验证电话号码");
			Logger.verifyEquals("100188", addressInfo.get("sAddressCode"), "验证Address code");
			Logger.verifyEquals("autotest" + userId+"updated", addressInfo.get("sAddress1"), "验证base address");
			Logger.verifyEquals("autotest" + userId+"detailupdated", addressInfo.get("sAddress2"), "验证detail address");
			Logger.verifyEquals(moblie, addressInfo.get("sCellPhone"), "验证Mobile信息");
			Logger.verifyEquals("100112", addressInfo.get("sPostCode"), "验证邮编");
			Logger.verifyEquals("updatedemail@ymatou.com", addressInfo.get("sEmail"), "验证邮箱地址");
			Logger.verifyEquals("autotest", addressInfo.get("sRecipient"), "验证recipient");
			Logger.verifyEquals(true, addressInfo.get("bIsDefault"), "验证地址是默认地址");
			Logger.verifyEquals(0, addressInfo.get("iAction"), "验证地址状态");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_AddressRequestDto_007() {
		Logger.start(true,"更新地址信息号码175段，成功-工单GD201610241679");
		try {
			
			Map user = UserCallService.registerByMobile();
			int userId = (Integer)user.get("userid");
			List<Map> address = addressWapper.getAdressByUserId(userId);
			
			arc = new AddressRequestDtoCall();
			arb.setiAddressId((Integer)address.get(0).get("iAddressId"));
			arb.setiUserid(userId);
			arb.setsAddressCode("100188");
			arb.setsBaseAddress("autotest" + userId+"updated");
			arb.setsDetailAddress("autotest" + userId+"detailupdated");
			arb.setiAction(ADDRESS_ACTIVE);
			arb.setsPostCode("610000");
			arb.setsTelphone("17502838882");
			arb.setsMobilePhone("17502838882");
			arb.setsRecipient("autotest");
			arb.setsEmail("updatedemail@ymatou.com");
			arb.setbIsDefault(true);
			arb.setAction(REQUEST_UPDATE);
			arc.setData(arb);
			arc.callService();
			Logger.verifyEquals(true, arc.getSuccess(), "修改地址成功");
			Logger.verifyEquals("ok", arc.getString("SuccessMessage"), "验证Msg信息");
			Logger.verifyEquals(ECode.SUCCESS, arc.getString("ResponseCode"), "验证Code");

			address = addressWapper.getAdressByUserId(userId);
			Logger.verifyEquals(1, address.size(), "验证只有一条地址信息");
			Map addressInfo = address.get(0);
			Logger.verifyEquals("17502838882", addressInfo.get("sTelphone"), "验证电话号码");
			Logger.verifyEquals("100188", addressInfo.get("sAddressCode"), "验证Address code");
			Logger.verifyEquals("autotest" + userId+"updated", addressInfo.get("sAddress1"), "验证base address");
			Logger.verifyEquals("autotest" + userId+"detailupdated", addressInfo.get("sAddress2"), "验证detail address");
			Logger.verifyEquals("17502838882", addressInfo.get("sCellPhone"), "验证Mobile信息");
			Logger.verifyEquals("610000", addressInfo.get("sPostCode"), "验证邮编");
			Logger.verifyEquals("updatedemail@ymatou.com", addressInfo.get("sEmail"), "验证邮箱地址");
			Logger.verifyEquals("autotest", addressInfo.get("sRecipient"), "验证recipient");
			Logger.verifyEquals(true, addressInfo.get("bIsDefault"), "验证地址是默认地址");
			Logger.verifyEquals(0, addressInfo.get("iAction"), "验证地址状态");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_AddressRequestDto_008() {
		Logger.start(false,"更新地址userid不匹配，失败");
		try {
			
			Map user = UserCallService.registerByMobile();
			int userId = (Integer)user.get("userid");
			List<Map> address = addressWapper.getAdressByUserId(userId);
			String moblie=TestDataManager.getPhoneNum();
			userId=1;
			arc = new AddressRequestDtoCall();
			arb.setiAddressId((Integer)address.get(0).get("iAddressId"));
			arb.setiUserid(userId);
			arb.setsAddressCode("100188");
			arb.setsBaseAddress("autotest" + userId+"updated");
			arb.setsDetailAddress("autotest" + userId+"detailupdated");
			arb.setiAction(ADDRESS_ACTIVE);
			arb.setsPostCode("100112");
			arb.setsTelphone("100101");
			arb.setsMobilePhone(moblie);
			arb.setsRecipient("autotest");
			arb.setsEmail("updatedemail@ymatou.com");
			arb.setbIsDefault(true);
			arb.setAction(REQUEST_UPDATE);
			arc.setData(arb);
			arc.callService();
			Logger.verifyEquals(false, arc.getSuccess(), "修改地址成功");
			Logger.verifyEquals("地址用户不匹配", arc.getString("LastErrorMessage"), "验证LastErrorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}